<script setup>
import * as echarts from 'echarts'
import { ref, onMounted } from 'vue'
const scatter = ref(null)
const props = defineProps({
  data: {
    type: Array
  }
})

const data = ref(
  props.data.map((item) => {
    return {
      type: 'scatter',
      name: item.title,
      data: [[item.point.lng, item.point.lat]]
    }
  })
)

const getMaxAndMin = (data, type) => {
  let n = type === 'lng' ? 0 : 1
  const arr = data.map((item) => {
    return item.data[0][n]
  })
  console.log(arr)
  return {
    min: Math.min(...arr).toFixed(3),
    max: Math.max(...arr).toFixed(3)
  }
}

const option = (data) => {
  let legend = data.map((item) => {
    return item.name
  })
  return {
    title: {
      text: '高校经纬度散点偏差',
      x: 'right', //可设定图例在左、右、居中
      y: 'top', //可设定图例在上、下、居中
      padding: [5, 20, 10, 10],
      textStyle: {
        fontSize: 10
      }
    },
    grid: {
      top: '10%',
      left: -0,
      right: '50%',
      bottom: '15%',
      containLabel: true
    },
    legend: {
      data: legend,
      orient: 'horizontal',
      x: 'right', //可设定图例在左、右、居中
      y: 'top', //可设定图例在上、下、居中
      padding: [25, -50, 0, 0],
      textStyle: {
        color: '#666',
        fontSize: 7
      },
      width: '10%',
      itemGap: 5,
      itemWidth: 5,
      itemHeight: 10
    },

    xAxis: {
      type: 'log',
      name: 'lng',
      minorSplitLine: {
        show: true
      },
      logBase: 10,
      min: getMaxAndMin(data, 'lng').min, //取最小值为最小刻度
      max: getMaxAndMin(data, 'lng').max //取最大值为最大刻度,
    },
    tooltip: {
      trigger: 'axis',
      axisPointer: {
        type: 'cross'
      }
    },
    yAxis: {
      type: 'log',
      name: 'lat',
      logBase: 10,
      minorSplitLine: {
        show: true
      },
      min: getMaxAndMin(data, 'lat').min, //取最小值为最小刻度
      max: getMaxAndMin(data, 'lat').max, //取最大值为最大刻度,
      splitLine: { show: true }
    },
    series: data
  }
}

onMounted(() => {
  const chart = echarts.init(scatter.value)
  console.log(data.value)
  chart.setOption(option(data.value))
})
</script>

<template>
  <div ref="scatter" style="width: 260px; height: 180px"></div>
</template>

<style lang="scss" scoped></style>
